package a66weding.com.jiehuntong.widgets.quicksearchlist;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;


import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by Administrator on 2016/6/11.
 */
public class QuickIndexView extends View {
    private Context mContext;
    private Paint paint;
    private int itemwidth;
    float itemHeight;
    int width;
    int height;
    private int index=-1;
    private String[] indexArr = { "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };
    //为了向Activity传递信息，定义一个监听器对象为类的成员
    private OnIndexChangeListener onIndexChangeListener;

    public QuickIndexView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
    }

    public QuickIndexView(Context context, AttributeSet attrs, Context mContext) {
        super(context, attrs);
        this.mContext = mContext;
        paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);

    }

    public QuickIndexView(Context context, AttributeSet attrs, int defStyleAttr, Context mContext) {
        super(context, attrs, defStyleAttr);
        this.mContext = mContext;
        paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        itemwidth = getMeasuredWidth();
        itemHeight = (float) getMeasuredHeight()/indexArr.length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //先画一个字符：
        paint.setTextSize(DensityUtil.dip2px(10));
        //     canvas.drawText("A",itemwidth/2,itemHeight/2,paint);
        width = getWidth();
        height = getHeight();
        int screenHeight = getScreenHeight();




        int itemWidth=width;
        //  float itemHeight=(float)height/26;
        for(int i=0;i<indexArr.length;i++){
            if(i==index){
                paint.setColor(Color.GRAY);
            }else{
                paint.setColor(Color.RED);
            }
            String word=indexArr[i];
            //得到字母的宽高
            Rect rect = new Rect();
            paint.getTextBounds(word,0,1,rect);

            int width = rect.width();
            int height = rect.height();

            canvas.drawText(word,(float) itemwidth/2,itemHeight*(i+1),paint);
        }



    }

    float eventX;
    float eventY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

            case MotionEvent.ACTION_MOVE:
                //获取事件坐标
                eventY = event.getY();
                int itemY = (int) (eventY / itemHeight);
                if(itemY!=index){
                    index=itemY;
                    invalidate();
                    //View视图对象发生某事件时，调用接口成员变量的回调方法
                    if (onIndexChangeListener != null) {
                        onIndexChangeListener.onIndexChanged(indexArr[index]);
                    }

                }

                break;
            case MotionEvent.ACTION_UP:
                index=-1;
                invalidate();
                if (onIndexChangeListener != null) {
                    onIndexChangeListener.onTouchUp();
                }
                break;

        }

        return true;
    }

    /**
     * 定义该接口类型的成员变量的set方法，提供给Activity调用，作为View和Activity联系的桥梁
     * @param onIndexChangeListener
     */
    public void setOnIndexChangeListener(OnIndexChangeListener onIndexChangeListener) {
        this.onIndexChangeListener = onIndexChangeListener;
    }

    /**
     * 定义接口，定义某事件发生时调用的方法（回调）
     */
    public interface OnIndexChangeListener{
        //当操作字母的index改变时，调用如下的回调方法：
        public void  onIndexChanged(String word);
        public void  onTouchUp();

    }

    public int getScreenHeight(){
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();//屏幕宽度
        int height = wm.getDefaultDisplay().getHeight();//屏幕高度
        return height;
    }


}
